package com.zjitc.pojo.exp;

import com.zjitc.pojo.Orders;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@SuppressWarnings("ALL")
public class DemoExpress {
    private static long HourInMilli = 1000 * 60 * 60;
    private static long selfHour = 1000 * 60 * 30;
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static ExpResult overwrite(ExpResult expResult, Orders orders) throws ParseException {
        List<Traces> newTraces = new ArrayList<>();
        long time = dateFormat.parse(orders.getCreateTime()).getTime();
        List<Traces> traces = writeTraces(mistiming(time), orders.getAddress(), orders.getName(), time);
        expResult.setTraces(traces);
        return expResult;
    }


    private static List<Traces> writeTraces(int state, String address, String name, long time) {
        List<Traces> traces = new ArrayList<>();
        if (!checkIsTongC(address)) {
            List<Traces> list = WaishengInit(address, name, time);
            traces = list.subList(0, state + 1);
        } else {
            int i = TongchengMis(time);
            List<Traces> list = TongchengInit(address, name, time);
            traces = list.subList(0, i+1);
        }
        return traces;
    }

    private static boolean checkIsTongC(String address) {
        if(address.contains("温州")){
            return true;
        }
        return false;
    }

    private static List<Traces> WaishengInit(String address, String name, long time) {
        String[] s = address.split(" ");
        String miniaddr1 = s[0] + s[1];
        String miniaddr2 = s[0] + s[1] + s[2];
        ArrayList list = new ArrayList();
        String state0 = dateFormat.format(new Date((long) (time + Math.floor(Math.random() * selfHour))));
        list.add(new Traces("商家已发货 正在等待揽收", state0));
        String state1 = dateFormat.format(new Date((long) (time + 2 * HourInMilli + Math.floor(Math.random() * selfHour))));
        list.add(new Traces("到达: 浙江省温州市市场部TYH服务部 已揽件", state1));
        String state2 = dateFormat.format(new Date((long) (time + 3 * HourInMilli + Math.floor(Math.random() * HourInMilli * 2))));
        list.add(new Traces("到达：浙江省温州市分拨中心", state2));
        String state3 = dateFormat.format(new Date((long) (time + 6 * HourInMilli + Math.floor(Math.random() * HourInMilli * 5))));
        list.add(new Traces("到达：浙江省温州市分拨中心 发往： " + miniaddr1 + "分拨中心", state3));
        String state4 = dateFormat.format(new Date((long) (time + 15 * HourInMilli + Math.floor(Math.random() * HourInMilli * 3))));
        list.add(new Traces("到达：" + miniaddr1 + "分拨中心 发往： " + miniaddr2 + "服务区", state4));
        String state5 = dateFormat.format(new Date((long) (time + 19 * HourInMilli + Math.floor(Math.random() * HourInMilli * 3))));
        list.add(new Traces("到达：" + miniaddr2 + "服务区 ", state5));
        String state6 = dateFormat.format(new Date((long) (time + 23 * HourInMilli + Math.floor(Math.random() * HourInMilli * 4))));
        list.add(new Traces("已签收： 收货人" + name + ",快递员 张**（15865468345）", state6));
        return list;
    }

    private static List<Traces> TongchengInit(String address, String name, long time) {
        String[] s = address.split(" ");
        String miniaddr1 = s[0] + s[1];
        String miniaddr2 = s[0] + s[1] + s[2];
        ArrayList list = new ArrayList();
        String time0 = dateFormat.format(new Date((long) (time + Math.floor(Math.random() * selfHour))));
        list.add(new Traces("商家已发货 正在等待揽收", time0));
        String time1 = dateFormat.format(new Date((long) (time + 2 * HourInMilli + Math.floor(Math.random() * selfHour))));
        list.add(new Traces("到达: 浙江省温州市市场部TYH服务部 已揽件", time1));
        String time2 = dateFormat.format(new Date((long) (time + 3 * HourInMilli + Math.floor(Math.random() * selfHour))));
        list.add(new Traces("到达: 同城转运中 配送人 王**(15957324854)", time2));
        String time6 = dateFormat.format(new Date((long) (time + 4 * HourInMilli + Math.floor(Math.random() * HourInMilli * 1))));
        list.add(new Traces("已签收： 收货人" + name + ",配送人 王**（15957324854）", time6));
        return list;
    }

    private static int mistiming(Long old) {
        long diff = (System.currentTimeMillis() - old) % HourInMilli;
        if (diff < 2) {
            return 0;
        } else if (diff < 3) {
            return 1;
        } else if (diff < 6) {
            return 2;
        } else if (diff < 16) {
            return 3;
        } else if (diff < 20) {
            return 4;
        } else if (diff < 23) {
            return 5;
        } else {
            return 6;
        }
    }

    private static int TongchengMis(Long old) {
        long diff = (System.currentTimeMillis() - old) % HourInMilli;
        if (diff < 2) {
            return 0;
        } else if (diff < 3) {
            return 1;
        } else if (diff < 4) {
            return 2;
        } else {
            return 3;
        }
    }





    public static void main(String[] args) throws Exception {

    }
}
